package com.app.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.app.mapper.CityMapper;
import com.app.mapper.ProvinceMapper;
import com.app.model.AreaModel;
import com.app.mapper.AreaMapper;
import com.app.model.CityModel;
import com.app.model.ProvinceModel;
import com.app.service.AreaService;
import com.app.vo.AreaVo;
import com.app.vo.ProvinceVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 行政区域县区信息表
 * </p>
 *
 * @author 16693103420
 * @since 2024-01-22
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaModel> implements AreaService {
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CityMapper cityMapper;

    @Override
    public List<AreaVo> getAreaList() {
        List<AreaVo> provincesModelList = areaMapper.getProvincesList();
        List<AreaVo> citiesModelList = areaMapper.getCitiesList();
        List<AreaVo> areasModelList = areaMapper.getAreasList();
        List<AreaVo> voList = new ArrayList<>();
        for (AreaVo province : provincesModelList) {//省级查询
            AreaVo vo = new AreaVo();
            vo.setValue(province.getProvinceId());
            vo.setLabel(province.getProvince());
            List<AreaVo> citiesList = new ArrayList<>();
            for (AreaVo citiesModel : citiesModelList) {//市级查询
                if (Objects.equals(province.getProvinceId(), citiesModel.getProvinceId())) {
                    AreaVo citiesVo = new AreaVo();
                    citiesVo.setValue(citiesModel.getCityId());
                    citiesVo.setLabel(citiesModel.getCity());
                    citiesList.add(citiesVo);
                    List<AreaVo> areasList = new ArrayList<>();
                    for (AreaVo areasModel : areasModelList) {
                        if (areasModel.getCityId().equals(citiesModel.getCityId())) {
                            AreaVo areasVo = new AreaVo();
                            areasVo.setValue(areasModel.getAreaId());
                            areasVo.setLabel(areasModel.getArea());
                            areasList.add(areasVo);
                        }
                    }
                    citiesVo.setChildren(areasList);
                }

            }
            vo.setChildren(citiesList);
            voList.add(vo);

        }
        return voList;
//        for (AreaVo areaVo : citiesModelList) {
//            char c = PinyinUtil.getFirstLetter(areaVo.getCity().charAt(0));
//            System.out.println(c);
//        }
//        return null;
    }

    @Override
    public Object listByKeyWords(String keywords) {
        QueryWrapper<ProvinceModel> provinceModelQueryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keywords)){
            provinceModelQueryWrapper.like("province", keywords);
        }
        List<ProvinceModel> list = provinceMapper.selectList(provinceModelQueryWrapper);
        List<ProvinceVo> vos = new ArrayList<>();
        Map<String, List<ProvinceModel>> groupByWords = list.stream()
                .collect(Collectors.groupingBy(ProvinceModel::getWords));
        for (ProvinceModel vehicleBrand : list) {
            ProvinceVo vo = new ProvinceVo();
            List<ProvinceModel> vehicleBrands = groupByWords.get(vehicleBrand.getWords());
            vo.setList(vehicleBrands);
            vo.setWord(vehicleBrand.getWords());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<ProvinceModel> listByWords(String word) {
        QueryWrapper<ProvinceModel> provinceModelQueryWrapper = new QueryWrapper<>();
        provinceModelQueryWrapper.eq("words", word);
        return provinceMapper.selectList(provinceModelQueryWrapper);
    }

    @Override
    public Object getCityById(Integer id) {
        QueryWrapper<CityModel> cityModelQueryWrapper = new QueryWrapper<>();
        cityModelQueryWrapper.eq("province_id", id);
        return cityMapper.selectList(cityModelQueryWrapper);
    }

    @Override
    public Object getAreaById(Integer id) {
        QueryWrapper<AreaModel> areaModelQueryWrapper = new QueryWrapper<>();
        areaModelQueryWrapper.eq("city_id", id);
        return areaMapper.selectList(areaModelQueryWrapper);
    }

    @Override
    public Object getHotCity() {
        QueryWrapper<CityModel> cityModelQueryWrapper = new QueryWrapper<>();
        cityModelQueryWrapper.eq("is_hot", 1);
        return cityMapper.selectList(cityModelQueryWrapper);
    }

    @Override
    public List<AreaVo> getCityList() {
        List<AreaVo> provincesModelList = areaMapper.getProvincesList();
        List<AreaVo> citiesModelList = areaMapper.getCitiesList();
        List<AreaVo> voList = new ArrayList<>();
        for (AreaVo province : provincesModelList) {//省级查询
            AreaVo vo = new AreaVo();
            vo.setValue(province.getProvinceId());
            vo.setLabel(province.getProvince());
            List<AreaVo> citiesList = new ArrayList<>();
            for (AreaVo citiesModel : citiesModelList) {//市级查询
                if (Objects.equals(province.getProvinceId(), citiesModel.getProvinceId())) {
                    AreaVo citiesVo = new AreaVo();
                    citiesVo.setValue(citiesModel.getCityId());
                    citiesVo.setLabel(citiesModel.getCity());
                    citiesList.add(citiesVo);
                }
            }
            vo.setChildren(citiesList);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<AreaVo> getCountyList() {
        return areaMapper.getAreasList();
    }
}
